package com.ruoyi.hospital.service.impl;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.hospital.*;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hospital.mapper.MrOutDataMapper;
import com.ruoyi.hospital.service.IMrOutDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MrOutDataServiceImpl implements IMrOutDataService {

    @Autowired
    MrOutDataMapper mrOutDataMapper;

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public List<OutPatList> getOutPatList(OutPatList outPatList) {
        return mrOutDataMapper.getOutPatList(outPatList);
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public int getOutPatListCount(OutPatList outPatList) {
        return mrOutDataMapper.getOutPatListCount(outPatList);
    }

    @Override
    public int addOutPatList(OutPatList outPatList) {
        return mrOutDataMapper.insertHpOutpatlist(outPatList);
    }

    @Override
    public int deletedOutPatList(String id) {
        return mrOutDataMapper.deletedOutPatList(id);
    }

    @Override
    public int updateOutPatList(OutPatList outPatList) {
        return mrOutDataMapper.updateHpOutpatlist(outPatList);
    }

    @Override
    public int saveMrOutData(MrOutData mrOutData) {
        Integer i = mrOutDataMapper.insertHpMroutbaisc(mrOutData.getMrOutBaisc());
        if(StringUtils.isNotNull(mrOutData.getMrOutRecord())){
            mrOutData.getMrOutRecord().setId(mrOutData.getMrOutBaisc().getOnlyId());
            mrOutDataMapper.insertHpMroutrecord(mrOutData.getMrOutRecord());
        }
        if(StringUtils.isNotNull(mrOutData.getMrOutDiagnosss())){
            for(MrOutDiagnoss mrOutDiagnoss: mrOutData.getMrOutDiagnosss().getMrOutDiagnossList()){
                mrOutDiagnoss.setId(mrOutData.getMrOutBaisc().getOnlyId());
                mrOutDataMapper.insertHpMroutdiagnoss(mrOutDiagnoss);
            }
        }
        if(StringUtils.isNotNull(mrOutData.getMrOutOrders())){
            for(MrOutOrder mrOutOrder: mrOutData.getMrOutOrders().getMrOutOrderList()){
                mrOutOrder.setId(mrOutData.getMrOutBaisc().getOnlyId());
                mrOutDataMapper.insertHpMroutorder(mrOutOrder);
            }
        }
        if(StringUtils.isNotNull(mrOutData.getMrOutPhysicalsigns())){
            for(MrOutPhysicalsign mrOutPhysicalsign: mrOutData.getMrOutPhysicalsigns().getMrOutPhysicalsignList()){
                mrOutPhysicalsign.setId(mrOutData.getMrOutBaisc().getOnlyId());
                mrOutDataMapper.insertHpMroutphysicalsign(mrOutPhysicalsign);
            }
        }
        return i;
    }

    @Override
    public void deleteMrOutData(String onlyId) {
        mrOutDataMapper.deleteMrOutBaisc(onlyId);
        mrOutDataMapper.deleteMrOutRecord(onlyId);
        mrOutDataMapper.deleteMrOutDiagnoss(onlyId);
        mrOutDataMapper.deleteMrOutOrder(onlyId);
        mrOutDataMapper.deleteMrOutPhysicalsign(onlyId);
    }

    @Override
    public void deleteMrOutBaisc(MrOutBaisc mrOutBaisc) {
        mrOutDataMapper.deleteMrOutBaisc(mrOutBaisc.getOnlyId());
    }

    @Override
    public void saveMrOutBaisc(MrOutBaisc mrOutBaisc) {
        mrOutDataMapper.insertHpMroutbaisc(mrOutBaisc);
    }

    @Override
    public MrOutData queryByOnlyId(String onlyId) {
        MrOutData mrOutData = new MrOutData();
        MrOutBaisc mrOutBaisc = mrOutDataMapper.queryMrOutBaisc(onlyId);
        mrOutData.setMrOutBaisc(mrOutBaisc);

        MrOutRecord mrOutRecord = mrOutDataMapper.queryMrOutRecord(onlyId);
        if(StringUtils.isNotNull(mrOutRecord)){
            mrOutData.setMrOutRecord(mrOutRecord);
        }

        List<MrOutPhysicalsign> mrOutPhysicalsign = mrOutDataMapper.queryMrOutPhysicalsign(onlyId);
        if(StringUtils.isNotNull(mrOutPhysicalsign)){
            MrOutPhysicalsigns mrOutPhysicalsigns = new MrOutPhysicalsigns();
            mrOutPhysicalsigns.setMrOutPhysicalsignList(mrOutPhysicalsign);
            mrOutData.setMrOutPhysicalsigns(mrOutPhysicalsigns);
        }

        List<MrOutDiagnoss> mrOutDiagnoss = mrOutDataMapper.queryMrOutDiagnoss(onlyId);
        if(StringUtils.isNotNull(mrOutDiagnoss)){
            MrOutDiagnosss mrOutDiagnosss = new MrOutDiagnosss();
            mrOutDiagnosss.setMrOutDiagnossList(mrOutDiagnoss);
            mrOutData.setMrOutDiagnosss(mrOutDiagnosss);
        }

        List<MrOutOrder> mrOutOrder = mrOutDataMapper.queryMrOutOrder(onlyId);
        if(StringUtils.isNotNull(mrOutOrder)){
            MrOutOrders mrOutOrders = new MrOutOrders();
            mrOutOrders.setMrOutOrderList(mrOutOrder);
            mrOutData.setMrOutOrders(mrOutOrders);
        }
        return mrOutData;
    }

    @Override
    public int saveLog(PostResult postResult) {
        return mrOutDataMapper.saveLog(postResult);
    }
}
